<template>
  <div class="box">
    <!-- 导航 -->
    <div class="title">
      <div class="title_fan row-wrapper">
        <span class="el-icon-arrow-left" @click="$router.go(-1)"></span>
        <span>{{ $t('Invite Code') }}</span>
        <span></span>
      </div>
      <div class="title_back_img">
        <div class="mini_back">
          <div>
            <img v-show="$route.query.type==='pay'"
                 src="../../../../assets/static/payQrcode/pay.png" alt="">
          </div>
          <div class="row-wrapper" style="justify-content: center">
            <span class="title-sk">{{ invitationCode }}</span>
            <div v-clipboard:copy="invitationCode" @click="copy">
              <img class="copyIcon"
                   src="../../../../assets/static/invitation/sk.png" alt=""/>
            </div>
          </div>
          <div style="width: 100%" class="row-wrapper">
            <span class="font-invitation">{{ $t('My Invite Code') }}</span>
          </div>
        </div>
        <div class="big_back">
          <div style="justify-content: center" class="qrcode col-wrapper">
            <div class="col-wrapper">
              <div>
                <span class="inputInvitation">{{ $t('Enter Invite Code') }}</span>
              </div>
              <div>
                <input v-model="otherInvitation" class="input"/>
              </div>
              <div @click="bandinvite" class="inputButton col-wrapper">
                <span>{{ $t('Confirm') }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {paytoken, paycode, recivecode} from '@/api/merbership/pay'
import {info} from '@/api/merbership/member'
import {getUid} from '@/utils/tokenTools'
import {searchorder} from '@/api/merbership/userpay'
import {balance} from '@/api/merbership/user'
import '@/styles/flax.css'
import {bandinvite} from '@/api/merbership/my'

export default {
  data() {
    return {
      payToken: '',
      qrcode: '',
      flushTime: 30,
      order_id: 1,
      orderInterval: null,
      interval: null,
      balacn: '0.00',
      invitationCode: '',
      otherInvitation: ''
    }
  },
  computed: {
    progress() {
      if (this.flushTime === 30 || this.flushTime === 0) {
        return {
          'width': String(this.flushTime / 30 * 100) + '%',
          'transition': 'width 0s'
        }
      } else {
        return {
          'width': String(this.flushTime / 30 * 100) + '%',
          'transition': 'width 2s'
        }
      }
    }
  },
  activated() {
    this.getInfo()
  },
  beforeDestroy() {
    clearInterval(this.interval)
  },
  methods: {
    copy() {
      this.$message.success(this.$t('复制成功'))
    },
    /**
     * 定时轮询订单是否支付成功
     */
    searchorderInterval() {
      this.orderInterval = setInterval(() => {
        this.searchorder()
      }, 3000)
    },
    /**
     * 二维码刷新倒计时
     */
    countDown() {
      let that = this
      this.interval = setInterval(() => {
        that.flushTime -= 1
      }, 1000)
    },
    // -------------------- axios ------------------------
    /**
     * 绑定邀请码
     */
    bandinvite() {
      const pd = {
        invite: this.otherInvitation
      }
      bandinvite(this.$qs.stringify(pd))
        .then(res => {
          if (res.data.code === 1) {
            this.$message.success(res.data.msg)
          } else {
            this.$message.error(res.data.msg)
          }
        })
    },
    /**
     * 会员详情
     */
    getInfo() {
      const pd = {}
      info(pd)
        .then(res => {
          if (res.data.code === 1) {
            this.invitationCode = res.data.data.invite
          } else {
            this.$message.error(res.data.msg)
          }
        })
    },
    /**
     * 收款二维码
     */
    recivecode() {
      const pd = {}
      recivecode(this.$qs.stringify(pd))
        .then(res => {
          this.qrcode = res.data.data
        })
    },

    /**
     * 用户余额
     */
    balance() {
      const pd = {}
      balance(this.$qs.stringify(pd))
        .then(res => {
          this.balacn = res.data.data.balance
        })
    },
    /**
     * 查询订单是否支付成功
     */
    searchorder() {
      const pd = {
        order_id: this.order_id
      }
      searchorder(this.$qs.stringify(pd))
        .then(res => {
          this.$message.info(res.data.msg)
        })
    },
    /**
     *
     * 支付二维码
     */
    paycode() {
      const pd = {
        token: this.payToken
      }
      paycode(this.$qs.stringify(pd))
        .then(res => {
          this.qrcode = res.data.data
        })
    },
    /**
     * 支付参数pay token
     */
    paytoken() {
      const pd = {
        order_id: getUid()
      }
      paytoken(this.$qs.stringify(pd))
        .then(res => {
          this.payToken = res.data.data
          if (this.$route.query.type === 'pay') {
            this.paycode()
          } else {
            this.recivecode()
          }
        })
    }
  },
  beforeDistory() {
    clearInterval(this.orderInterval)
  },
  watch: {
    flushTime(val) {
      if (val === 0) {
        clearInterval(this.interval)
        this.flushTime = 30
        this.countDown()
        this.paytoken()
      }
    }
  }
}
</script>
<style scoped>
.copyIcon {
}

.title-sk {
  font-size: 30px;
  font-weight: 500;
  color: #333333;
  line-height: 31px;
}

.title-sk + div > img {
  width: 20px;
  height: 20px;
  margin-left: 10px;
}

.qrcode {
  height: 300px;
}

.font-invitation {
  font-size: 15px;
  font-weight: 500;
  color: #999999;
  line-height: 31px;
}

.inputInvitation {
  font-size: 20px;
  font-weight: 500;
  color: #000000;
  line-height: 31px;
}

.input {
  margin-top: 28px;
  background: #FFFFFF;
  border: 1px solid #CBCBCB;
  padding-left: 15px;
  border-radius: 12px;
  outline: none;
  width: 250px;
  height: 44px;
}

.inputButton {
  margin-top: 24px;
  width: 250px;
  height: 45px;
  justify-content: center;
  background: linear-gradient(-8deg, #FFB200, #FED000);
  box-shadow: 0 9px 19px 0 rgba(254, 208, 0, 0.15);
  border-radius: 16px;
}

.inputButton > span {
  font-size: 14px;
  font-weight: 500;
  color: #FFFFFF;
  line-height: 31px;
}
</style>
<style lang="scss" scoped>
.box {
  padding-bottom: 6%;

  .title {
    background-image: linear-gradient(to bottom, #7AC91B 25%, #f5f5f5 0);

    .title_fan {
      justify-content: space-between;
      color: #fff;
      font-size: 18px;
      padding: 15px 1%;
    }
  }

  .title_back_img {
    background-image: url(../../../../assets/static/membership/a5.png);
    background-repeat: no-repeat;
    background-size: 100% 100%;
    padding: 5px 0;

    .mini_back {
      text-align: center;
      font-size: 15px;
      padding: 9% 0;

      > div:nth-child(1) {
        > div > img {
          width: 45px;
        }
      }

      > div:nth-child(3) {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 15px;

        img {
          width: 25px;
          height: 25px;
        }

        span {
          font-weight: 600;
        }
      }
    }

    .big_back {
      margin: 15%;
      text-align: center;
      font-size: 11px;

      img {
        width: 90%;
      }

      .progress {
        margin: 5px 0;
        width: 100%;
        height: 10px;
        padding: 5px;
        background-image: url(../../../../assets/static/membership/a2.png);
        background-size: 100% 100%;
        background-repeat: no-repeat;

        .back_progress {
          background-color: #FFB200;
          // width: 30%;
          height: 10px;
          border-radius: 15px;
        }
      }
    }
  }
}
</style>
